<?php
namespace Home\Controller;
use Think\Controller;

class OrderController extends \Common\Controller\UserController {
	public function index() {
		$Gethelp = M('gethelp');
		$userid = session('userid');
		$Gethelps = $Gethelp -> where('status = 0 and other_status = 1 and userid != ' . $userid) -> order('id DESC') -> select();

		$User = M('user');
		foreach($Gethelps as &$v) {
			$v['user'] = $User -> field('id, mobile, realname, bank, bankcode, bankname') -> where('id = ' . $v['userid']) -> find();
		}
		$UserGethelps = $Gethelp -> where('other_status = 2 and get_order_userid = ' . $userid) -> order('id ASC') -> select();

		if(!empty($UserGethelps)) {
			$this -> assign('UserGethelps', $UserGethelps);
		}
		$this -> assign('Gethelps', $Gethelps);
        $this -> display();
	}

	public function get_order() {
		$gethelp_id = I('get.gethelp_id');
		$Gethelp = M('gethelp');
		$gethelp = $Gethelp -> where('id = ' . $gethelp_id) -> find();
		$userid = session('userid');

		if(IS_POST) {
			$post = I('post.');

			$User = M('user') -> field('id, paypassword') -> where('id = ' . $userid) -> find();
	
			if(!(new \Think\PasswordHash()) -> CheckPassword(md5($post['paypassword']), $User['paypassword'])) {
				$this -> error('支付密码错误');exit;
			}

			$Gethelp -> where('id = ' . $post['gethelp_id']) -> save(array('status' => 3, 'other_status' => 0));

			$tohelpId = M('tohelp') -> add(array(
				'orderid' => $number='B'.str_pad($post['gethelp_id'], 9, "0", STR_PAD_LEFT) . '01',
				'userid' => $User['id'],
				'price'  => $gethelp['price'],
				'createtime' => time(),
				'status' => 3,
				'begintime' => time(),
				'interest'  => 0,
				'surplus'   => 0,
				'replay'    => 0,
				'gethelpid' => $post['gethelp_id']
			));
	
			if($tohelpId) {
				$parameters = F('para');
				(new UserController()) -> getfreeze($User['id'], $tohelpId, $gethelp['price'] + $gethelp['price'] * ($parameters[0]['number'] / 100), 0, $User['id'], 1, time(), '订单交易完成');

				M('toget') -> add(array(
					'toid' => $tohelpId,
					'getid' => $post['gethelp_id'],
					'createtime' => time(),
					'status' => 3
				));
	
				$this -> success('支付成功!', '/user/profile');
				exit;
			}
		}
		

		if($gethelp['get_order_userid'] != 0 && $gethelp['get_order_userid'] != $userid) {
			$this -> error('单已被抢！');
		} else {
			$Gethelp -> where('id = ' . $gethelp_id) -> save(array('other_status' => 2, 'get_order_userid' => $userid));
		}

		$Gethelp = M('gethelp') -> where('id = ' . $gethelp_id) -> find();

		$Gethelp['user'] = M('user') -> field('id, mobile, realname, bank, bankcode, bankname') -> where('id = ' . $Gethelp['userid']) -> find();

		$this -> assign('Gethelp', $Gethelp);

		$this -> display();
	}
	
	public function cancel_order() {
		$gethelp_id = I('get.gethelp_id');
		if(M('gethelp') -> where('id = ' . $gethelp_id) -> save(array('other_status' => 1, 'get_order_userid' => 0))) {
			$this -> redirect('/order');
		}
	}
}